package org.example.login2;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import java.io.IOException;

import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String major = req.getParameter("major");
        int regularScore = 0;
        int finalScore = 0;

        Student newStudent = new Student(id, name, major, regularScore, finalScore);
        try {
            InMemoryStudentDatabase.addStudent(newStudent);
            resp.sendRedirect("studentList");
        } catch (DuplicateStudentIdException e) {
            req.setAttribute("errorMessage", e.getMessage());
            req.getRequestDispatcher("/newStudent.jsp").forward(req, resp);
        }
    }

}
